;====================================================================
;
; Ogre3D Wrapper for Blitz3D
;
; Entities
;
;====================================================================
; Entity Types
Const ET_NO_ENTITY			= 0
Const ET_ENTITY				= 1
Const ET_CAMERA				= 2
Const ET_LIGHT				= 3
Const ET_SCENENODE			= 4
Const ET_MATERIAL			= 5
Const ET_TECHNIQUE			= 6
Const ET_MESH				= 7

Const CS_BOX				= 1
Const CS_SPHERE				= 2


Type TEntity
	Field typ%
	Field ptr%
	Field vport%		; for cameras
	Field lightType%	; for Lights
	Field rbody%		; RigidBody for Physics
End Type
Global ent.TEntity
Global ogre_entity_count%	= 0


Function PositionEntity(entity%, x#, y#, z#)
	Local e.TEntity = Object.TEntity(entity)
	
	Select e\typ
		Case ET_CAMERA		: o_camera_setPosition(e\ptr, x, y, -z)
		Case ET_LIGHT		: o_light_setPosition(e\ptr, x, y, -z)
		Case ET_ENTITY		: o_scenenode_setposition(o_entity_getParentSceneNode(e\ptr), x, y, -z)
		Case ET_SCENENODE	: o_scenenode_setposition(e\ptr, x, y, -z)
	End Select
	
End Function


Function TurnEntity(entity%, pitch#, yaw#, roll#)
	Local e.TEntity = Object.TEntity(entity)
	
	Select e\typ
		Case ET_CAMERA
			o_camera_pitch(e\ptr, pitch)
			o_camera_yaw(e\ptr, yaw)
			o_camera_roll(e\ptr, roll)
			
		Case ET_SCENENODE
			o_scenenode_pitch(e\ptr, pitch, TS_PARENT)
			o_scenenode_yaw(e\ptr, yaw, TS_PARENT)
			o_scenenode_roll(e\ptr, roll, TS_PARENT)
	End Select
	
End Function


Function MoveEntity(entity%, x#, y#, z#)
	Local e.TEntity = Object.TEntity(entity)
	
	Local pos.TVector3 = New TVector3
	pos\x = x
	pos\y = y
	pos\z = -z
	
	Select e\typ
		Case ET_CAMERA		: o_camera_moveRelative(e\ptr, pos)
		Case ET_ENTITY		: o_scenenode_translate(o_entity_getParentSceneNode(e\ptr), x, y, -z, TS_LOCAL)
		Case ET_SCENENODE	: o_scenenode_translate(e\ptr, x, y, -z, TS_LOCAL)
	End Select
End Function


Function CreatePivot%()
	ent.TEntity = New TEntity
	ogre_entity_count = ogre_entity_count + 1
	ent\ptr	= bb_createChildSceneNode(ogre3d\ogre_rootscenenode, genEntityName("pivot_"))
	ent\typ	= ET_SCENENODE
	
	Return Handle(ent)
End Function


Function EntityParent(entity%, parent%)
	Local e.TEntity = Object.TEntity(entity)
	Local p.TEntity = Object.TEntity(parent)
	Select p\typ
		Case ET_SCENENODE
			Select e\typ
				Case ET_CAMERA		: o_attachObject(p\ptr, e\ptr)
			End Select
	End Select
End Function


Function EntityShadow(entity%, cast_shadows% = True)
	Local e.TEntity = Object.TEntity(entity)
	o_entity_setCastShadows(e\ptr, cast_shadows)
End Function


Function EntityX#(entity%)
	Local e.TEntity = Object.TEntity(entity)
	
	Local result#	= 0.0
	Local sze%		= 0
	
	Select e\typ
		Case ET_ENTITY, ET_MESH	: sze = o_scenenode_getposition(o_entity_getParentSceneNode(e\ptr))
		Case ET_CAMERA			: sze = o_camera_getPosition(e\ptr)
		Case ET_LIGHT			: sze = o_light_getPosition(e\ptr)
	End Select
	
	If sze > 0
		CopyMemory(vector, sze, 12)
		result = PeekFloat(vector, 0)
	EndIf
	
	Return result
End Function


Function EntityY#(entity%)
	Local e.TEntity = Object.TEntity(entity)
	
	Local result#	= 0.0
	Local sze%		= 0
	
	Select e\typ
		Case ET_ENTITY, ET_MESH	: sze = o_scenenode_getposition(o_entity_getParentSceneNode(e\ptr))
		Case ET_CAMERA			: sze = o_camera_getPosition(e\ptr)
		Case ET_LIGHT			: sze = o_light_getPosition(e\ptr)
	End Select
	
	If sze > 0
		CopyMemory(vector, sze, 12)
		result = PeekFloat(vector, 4)
	EndIf
	
	Return result
End Function


Function EntityZ#(entity%)
	Local e.TEntity = Object.TEntity(entity)
	
	Local result#	= 0.0
	Local sze%		= 0
	
	Select e\typ
		Case ET_ENTITY, ET_MESH	: sze = o_scenenode_getposition(o_entity_getParentSceneNode(e\ptr))
		Case ET_CAMERA			: sze = o_camera_getPosition(e\ptr)
		Case ET_LIGHT			: sze = o_light_getPosition(e\ptr)
	End Select
	
	If sze > 0
		CopyMemory(vector, sze, 12)
		result = PeekFloat(vector, 8)
	EndIf
	
	Return result
End Function


Function FreeEntity(entity%)
	Local e.TEntity = Object.TEntity(entity)
	
	Select e\typ
		Case ET_CAMERA			: o_destroyCamera(ogre3d\ogre_scenemanager, e\ptr)
		Case ET_LIGHT			: o_destroyLight(ogre3d\ogre_scenemanager, e\ptr)
		Case ET_ENTITY, ET_MESH	: o_destroyEntity(ogre3d\ogre_scenemanager, e\ptr)
		Case ET_SCENENODE		: o_destroySceneNode(ogre3d\ogre_scenemanager, e\ptr)
	End Select
End Function


Function CopyEntity%(entity%, parent% = 0)
	Local e.TEntity = Object.TEntity(entity)
	Local p.TEntity = Object.TEntity(parent)
	
	Local ent.TEntity = New TEntity
	ogre_entity_count = ogre_entity_count + 1
	
	ent\ptr	= o_clone(e\ptr, genEntityName())
	ent\typ	= e\typ
	
	Local cn% = bb_createChildSceneNode(ogre3d\ogre_rootscenenode, genEntityName("node_"))
	o_attachObject(cn, ent\ptr)
	
	If parent > 0 Then EntityParent(Handle(ent), parent)
	
	Return Handle(ent)
End Function


Function DynamicEntity(entity%, mass% = 1.0, shapeType% = CS_BOX, playerControlled% = False)
	Local ent.TEntity = Object.TEntity(entity)
	Local s.TVector3 = New TVector3
	Local p.TVector3 = New TVector3
	
	s\x = MeshWidth(entity)
	s\y = MeshHeight(entity)
	s\z = MeshDepth(entity)
	
	p\x = EntityX(entity)
	p\y = EntityY(entity)
	p\z = EntityZ(entity)
	
	ent\rbody = createRigidBody(ogre3d\bullet, shapeType, mass, playerControlled, s, p)
End Function






;~IDEal Editor Parameters:
;~F#65#80#95
;~C#Blitz3D